import pandas as pd
import os
import libreface

def get_visual_feature_dimension(video_path, output_dir):
    """
    确定libreface输出的视觉特征维度。

    参数:
        video_path (str): 输入的视频文件路径。
        output_dir (str): 输出特征保存的目录。

    返回:
        int: 特征的维度（列数）。
    """
    # 确保输出目录存在
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 输出文件路径
    output_save_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_path))[0] + '_features.csv')

    # 运行libreface获取特征
    libreface.get_facial_attributes(video_path, output_save_path=output_save_path, temp_dir="../temp")

    # 读取CSV文件，获取列数
    if os.path.exists(output_save_path):
        df = pd.read_csv(output_save_path)
        feature_dim = df.shape[1]  # 列数即为特征维度
        print(f"Visual feature dimension: {feature_dim}")
        return feature_dim
    else:
        print("Error: CSV output not found.")
        return None


if __name__ == '__main__':
    # 示例视频路径
    sample_video_path = './data/video-labeled/sample_00000000.mp4'
    output_directory = './data/out_feature'

    # 确定视觉特征维度
    visual_dim = get_visual_feature_dimension(sample_video_path, output_directory)

    if visual_dim:
        print(f"The visual feature dimension is {visual_dim}.")
    else:
        print("Failed to determine visual feature dimension.")
